package com.hgkj.common.core.utils;


import com.hgkj.common.core.exception.ServiceException;

import java.util.Objects;

/**
 * 快速失败自定义异常抛出
 * @author hgkj
 */
public class FailFastUtils {

    /**
     * 相等快速失败
     * @param source 源
     * @param target 目标
     * @param failMsg 失败消息
     */
    public static void equalsFailFast(Object source, Object target, String failMsg){
        if(Objects.isNull(source) && Objects.isNull(target)){
            throw new ServiceException(failMsg);
        }
        if(Objects.isNull(source) || Objects.isNull(target)){
            return;
        }
        if(source.equals(target)){
            throw new ServiceException(failMsg);
        }
    }

    /**
     * 不相等快速失败
     * @param source 源
     * @param target 目标
     * @param failMsg 失败消息
     */
    public static void notEqualsFailFast(Object source, Object target, String failMsg) {
        if (Objects.isNull(source) && Objects.isNull(target)) {
            return;
        }
        if (Objects.isNull(source) || Objects.isNull(target)){
            throw new ServiceException(failMsg);
        }
        if (!source.equals(target)) {
            throw new ServiceException(failMsg);
        }
    }
}
